
package demo.issuetracker.controller;

import demo.issuetracker.model.CustomIdentity;
import demo.issuetracker.entity.User;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;


import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.event.ActionEvent;
import java.util.Map;
import java.util.List;

/**
 * Glue code to bind Home Service with the view framework (Seam). This class contains
 * a default implementation of the action methods. Please avoid reimplmenting the action methods in this class.
 * The purpose of this class is to declare properties which can be bound to the UI.
 * 
  * This page introduces the user to Issue Tracker and shows a login form.
  *
 * Automatically generated by clickframes
 * 
 * @author Vineet Manohar
 */
public abstract class GeneratedLoginController extends Controller {
    @In("#{userDao}") 
    protected demo.issuetracker.dao.UserDao userDao;

    @In("#{userService}") 
    protected demo.issuetracker.entity.service.UserService userService;




    /** $!User - INPUT */
    protected User user;

    /** 
     * Get User
     *  
     * @return User
     */
    public User getUser() {
       return this.user;
    }

    /**
     * Set User
     *
     * @param user
     */
    public void setUser(User user) {
        this.user = user;
    }


    protected void initUser() {
        // if ${outputList.id} has not been initialized, initialize it now
        if (getUser() == null) {
            setUser(loadUser());
        }
        
     }

    protected User loadUser() {
    return null;
    }

    protected void reloadUser() {
        clearUser();
        loadUser();
    }

    protected void clearUser() {
        setUser(null);
    }
 
    
	@SuppressWarnings("unused")
	private static final Log logger = LogFactory.getLog(GeneratedLoginController.class);

	@In
	protected CustomIdentity identity;

    @Create
    public void init() {


        // initialize all file inputs
 

        // initialize all file inputs
          // all inputs initialized here 
    }




	/**
	 * This method should be called before the page is loaded
	 * 
	 * All the page parameters should be consumed in this method
	 */    
    public String processPageParameters() {


          // if you need to add more code, override this method in the child class

        return null;
    }





	/** Username
	 *
 	 * Username
 	 */
   
	protected String loginFormUsername;

  	@org.hibernate.validator.NotNull(message="Please enter your username.")
  	public String getLoginFormUsername() {
	   return this.loginFormUsername;
	}

	public void setLoginFormUsername(String loginFormUsername) {
		this.loginFormUsername = loginFormUsername;
	}
	/** Password
	 *
 	 * Password
 	 */
   
	protected String loginFormPassword;

  	@org.hibernate.validator.NotNull(message="Please enter your password.")
  	public String getLoginFormPassword() {
	   return this.loginFormPassword;
	}

	public void setLoginFormPassword(String loginFormPassword) {
		this.loginFormPassword = loginFormPassword;
	}



 
       public void loginFormUsernameValidator(javax.faces.context.FacesContext facesContext,
            javax.faces.component.UIComponent component, java.lang.Object value) {
        String valueStr = value.toString();
        
    }
     public void loginFormPasswordValidator(javax.faces.context.FacesContext facesContext,
            javax.faces.component.UIComponent component, java.lang.Object value) {
        String valueStr = value.toString();
        
    }

 

 
    /**
     *  Converts any form inputs bound to entities to entities, stores them in the map ready to 
     *  be passed to the service methods
     */
    protected User getLoginFormUser() {
         // create a new User from input
        User newUser = userDao.newInstance();

        newUser.setUsername(toText(loginFormUsername));
        newUser.setPassword(toText(loginFormPassword));

        return newUser;
    }

    public LoginFormLoginOutcome loginFormLogin() {
      // logout first if already logged in
      if (identity.isLoggedIn(false)) {
          identity.logout();
      }

       // call seam
      String seamOutcome = identity.login();

      if (seamOutcome != null && seamOutcome.equals("loggedIn")) {
          return convertSuccessfulSeamOutcomeToApplicationOutcome();
      }

      // convert seam outcome into clickframes outcome
      return LoginFormLoginOutcome.loginFailed;
   }

   public boolean authenticate() {
     User user = userDao.findByUsernameAndPassword(loginFormUsername, loginFormPassword);
        
     // store user for the session
     identity.setUser(user);

     // identity.setRememberMe(false);
       
     // return outcome
     return user != null;
   }


    protected LoginFormLoginOutcome convertSuccessfulSeamOutcomeToApplicationOutcome() {
        return LoginFormLoginOutcome.loginSuccessful;
    }


    private static final long serialVersionUID = 1L;

     /**
     * different legal outcomes for Login 
     * 
     * This page introduces the user to Issue Tracker and shows a login form.
     */
     public enum LoginFormLoginOutcome {
        /** 
         * Login successful
         */ 
        loginSuccessful,    
        /** 
         * Login failed
         */ 
        loginFailed    
    }
  }// clickframes::version=1174650096::clickframes